package com.tsing.cloud.controller;

import com.tsing.cloud.service.OrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author TheTsing
 * @version 1.0
 * @date 2021/11/8 14:11
 * <p>
 * 降级方法：1.单个配置
 *      例：@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {
 *          @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")})
 * 2.全局服务降级
 *      例：类上标注@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
 *          需要降级的方法上标注@HystrixCommand
 * 3.通配服务降级
 *      例：yml配置
 *          #打开feign对hystrix的支持
 *          feign:
 *          hystrix:
 *          enabled: true
 *          service接口注解@FeignClient(value = "CLOUD-PAYMENT-HYSTRIX-SERVICE", fallback = OrderFallbackService.class)
 *          写一个该接口的实现类OrderFallbackService，其实现方法即为降级方法
 */
@RestController
//@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderController {

    /**
     * 服务降级：服务器忙，请稍后再试，不让客户端等待并立刻返回一个友好提示，fallback，情景：程序运行异常、超时、服务熔断触发服务降级、线程池、信号量打满
     * 服务熔断：达到最大服务访问后，直接拒绝访问，然后调用服务降级方法并返回友好提示，详见PaymentHystrixMain8001  PaymentService
     */
    @Resource
    private OrderService orderService;

    @GetMapping("/get/{id}")
    public List<Object> get(@PathVariable("id") String id) {
        return orderService.get(id);
    }

    @GetMapping("/timeout")
//    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")})
//    @HystrixCommand
    public String timeout() {
        return orderService.timeout();
    }

    public String payment_Global_FallbackMethod() {
        return "全局降级处理兜底方法";
    }

}
